package csu.web.credit_bank.service.impl;

import csu.web.credit_bank.mapper.CreditSummaryMapper;
import csu.web.credit_bank.pojo.CreditSummary;
import csu.web.credit_bank.service.CreditSummaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CreditSummaryServiceImpl extends ServiceImpl<CreditSummaryMapper, CreditSummary> implements CreditSummaryService {
    @Autowired
    private CreditSummaryMapper creditSummaryMapper;
    @Override
    public CreditSummary createCreditSummary(CreditSummary creditSummary) {
        this.save(creditSummary);
        return creditSummary;
    }

    @Override
    public CreditSummary getCreditSummaryByUserId(String userId) {
       CreditSummary creditSummary= creditSummaryMapper.selectById(userId);
//        System.out.println(creditSummary);
        if (creditSummary == null || userId.isEmpty()) {
            creditSummary = new CreditSummary();
            creditSummary.setUserId(userId);
            creditSummary.setCurrentCredits(0);
            creditSummary.setTotalCredits(0);
            creditSummary.setLastUpdateTime(new Date());
            creditSummaryMapper.insert(creditSummary);
            return creditSummary;
        }
        return creditSummaryMapper.selectById(userId);
    }

    @Override
    public CreditSummary updateCreditSummary(CreditSummary creditSummary) {
        this.updateById(creditSummary);
        return this.getById(creditSummary.getUserId());
    }

    @Override
    public boolean deleteCreditSummary(String userId) {
        return this.removeById(userId);
    }
}